package com.xxx.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.common.Result;

import com.xxx.dto.RequestDto;
import com.xxx.enums.StatusEnum;
import com.xxx.pojo.RequestInfo;
import com.xxx.pojo.User;
import com.xxx.service.RequestInfoService;
import com.xxx.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/request-info")
public class RequestInfoController {

    @Autowired
    private RequestInfoService requestInfoService;

    @Autowired
    private UserService userService;

    /**
     * 新增报价请求
     * @param requestInfo
     * @return
     */

    @Transactional
    @PostMapping("/save")
    @PermitAll
    public Result save(@RequestBody RequestInfo requestInfo){

        requestInfo.setUpdateTime(LocalDateTime.now());
        boolean save = requestInfoService.save(requestInfo);
        log.info("requestInfo:{}",requestInfo);

        return save? Result.success("提交成功") : Result.error("系统错误");
    }

    /**
     * 根据id删除
     * @param ids
     * @return
     */
    @Transactional
    @DeleteMapping("/{ids}")
    @PreAuthorize("hasAnyAuthority('sys:delete:request')")
    public Result delById(@PathVariable List<Long> ids){
        boolean b = requestInfoService.removeByIds(ids);
        return b? Result.success("删除成功") : Result.error("删除失败");
    }

    @PreAuthorize("hasAnyAuthority('sys:select:request')")
    @GetMapping("/get-info")
    public Result getInfo(Integer pageNo, Integer pageSize, String name, String charge){
        IPage iPage = new Page(pageNo,pageSize);

        List<Long> list = null;
        if(charge != null && !"".equals(charge.trim())){
            list = userService.list(new LambdaQueryWrapper<User>()
                            .like( User::getName, charge))
                    .stream().map(User::getUserId).collect(Collectors.toList());
        }

        iPage = requestInfoService.page(iPage, new LambdaQueryWrapper<RequestInfo>()
                .like(name != null && !"".equals(name), RequestInfo::getName, name)
                .in(list != null, RequestInfo::getCharge, list));

//        log.info("statusEnum:{}",distribution);
        List<RequestInfo> records = iPage.getRecords();
        //转换流
        List<RequestDto> collect = records.stream().map(requestInfo -> {
            RequestDto requestDto = new RequestDto();
            BeanUtils.copyProperties(requestInfo, requestDto);

            if (requestInfo.getCharge() != null) {
                User user = userService.getById(requestInfo.getCharge());
                requestDto.setUser(user);
            }
            return requestDto;
        }).collect(Collectors.toList());

        iPage.setRecords(collect);
        return Result.success("查询成功",iPage);
    }


    @PreAuthorize("hasAnyAuthority('sys:update:request')")
    @Transactional
    @PutMapping("/update")
    public Result update(@RequestBody RequestInfo requestInfo){

        log.info("requestInfo:{}",requestInfo);

        boolean b = requestInfoService.updateById(requestInfo);

        return b ? Result.success("修改成功") : Result.error("error");

    }

}
